/*
 *    Copyright (c) 2018-2025, workdata All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: workdata
 */

package com.workcell.workdata.xthings.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.workcell.workdata.common.core.util.R;
import com.workcell.workdata.common.excel.annotation.ResponseExcel;
import com.workcell.workdata.common.log.annotation.SysLog;
import com.workcell.workdata.xthings.dto.BaseVo;
import com.workcell.workdata.xthings.entity.BaseRotaryKiln;
import com.workcell.workdata.xthings.entity.Manufacturer;
import com.workcell.workdata.xthings.service.BaseRotaryKilnService;
import com.workcell.workdata.xthings.service.ManufacturerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 回转窑表
 *
 * @author pigx code generator
 * @date 2023-07-20 13:58:38
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/baserotarykiln" )
@Api(value = "baserotarykiln", tags = "回转窑表管理")
public class BaseRotaryKilnController {

    private final BaseRotaryKilnService baseRotaryKilnService;

    private final ManufacturerService manufacturerService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param baseRotaryKiln 回转窑表
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getBaseRotaryKilnPage(Page page, BaseRotaryKiln baseRotaryKiln) {
        return R.ok(baseRotaryKilnService.page(page, Wrappers.query(baseRotaryKiln)));
    }


    /**
     * 通过id查询回转窑表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) String id) {
        return R.ok(baseRotaryKilnService.getById(id));
    }

    /**
     * 新增回转窑表
     * @param baseRotaryKiln 回转窑表
     * @return R
     */
    @ApiOperation(value = "新增回转窑表", notes = "新增回转窑表")
    @PostMapping
    public R save(@RequestBody BaseRotaryKiln baseRotaryKiln) {

        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseRotaryKiln.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseRotaryKiln.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseRotaryKiln.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseRotaryKiln.setManufacturer(manufacturer.getManufacturerFullName());
        baseRotaryKiln.setManufacturerCode(manufacturer.getManufacturerCode());
        return R.ok(baseRotaryKilnService.save(baseRotaryKiln));
    }

    /**
     * 修改回转窑表
     * @param baseRotaryKiln 回转窑表
     * @return R
     */
    @ApiOperation(value = "修改回转窑表", notes = "修改回转窑表")
    @PutMapping
    public R updateById(@RequestBody BaseRotaryKiln baseRotaryKiln) {
        Manufacturer manufacturer = manufacturerService.selectByManufacturerCode(baseRotaryKiln.getDeviceManufacturer());
        Assert.notNull(manufacturer, "厂商查询为空");
        baseRotaryKiln.setManufacturerShortName(manufacturer.getManufacturerShortName());
        baseRotaryKiln.setManufacturerFullName(manufacturer.getManufacturerFullName());
        baseRotaryKiln.setManufacturer(manufacturer.getManufacturerFullName());
        return R.ok(baseRotaryKilnService.updateById(baseRotaryKiln));
    }

    /**
     * 通过id删除回转窑表
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除回转窑表", notes = "通过id删除回转窑表")
    @DeleteMapping("/{id}" )
    public R removeById(@PathVariable String id) {
        return R.ok(baseRotaryKilnService.removeById(id));
    }


    /**
     * 导出excel 表格
     * @param baseRotaryKiln 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    public List<BaseRotaryKiln> export(BaseRotaryKiln baseRotaryKiln) {
        return baseRotaryKilnService.list(Wrappers.query(baseRotaryKiln));
    }

    /**
     * 模糊分页查询
     * @param baseVo
     * @return
     */
    @ApiOperation(value = "模糊分页查询",notes = "模糊分页查询")
    @PostMapping("/vaguePage")
    public R vaguePage(@RequestBody BaseVo baseVo){
        return R.ok(baseRotaryKilnService.vaguePage(baseVo));
    }

    @ApiOperation(value = "修改回转窑状态信息", notes = "修改回转窑状态信息")
    @PutMapping("/updateStateById")
    public R updateStateById(@RequestBody BaseRotaryKiln baseRotaryKiln){
        return R.ok(baseRotaryKilnService.updateStateById(baseRotaryKiln));
    }

}
